package com.yf.service.meetingUser.impl;


import com.yf.dao.meetingUser.MeetingUserDao;
import com.yf.entity.UserEntity;
import com.yf.entity.WxUser;
import com.yf.entity.meetingUser.MeetingUserEntity;
import com.yf.enums.UserStatusEnum;
import com.yf.response.RRException;
import com.yf.service.impl.BaseServiceImpl;
import com.yf.service.meetingUser.MeetingUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 年会抽奖用户Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Thu Dec 13 10:32:50 CST 2018
 */
@Service
@Slf4j
public class MeetingUserServiceImpl extends BaseServiceImpl<MeetingUserEntity> implements MeetingUserService {
    @Autowired
    private MeetingUserDao meetingUserDao;



    /**内部方法**/


    /**外部方法实现**/
    public MeetingUserEntity findByOpenId(String openId) {
        List<MeetingUserEntity> list = meetingUserDao.findByOpenId(openId);
        if(list==null||list.isEmpty()){
            return null;
        }
        if(list.size()>1){
            log.error("账号异常unionId不唯一:"+openId);
            throw new RRException(openId+"账号不唯一");
        }
        return list.get(0);
    }

    @Transactional
    public void registerUser(WxUser wxUser) {
        MeetingUserEntity meetingUser = findByOpenId(wxUser.getOpenId());
        if(meetingUser==null){
            Date date = new Date();
            MeetingUserEntity meetingUserEntity = new MeetingUserEntity(wxUser.getNickName(),wxUser.getHeadImgUrl(),wxUser.getSex(),wxUser.getOpenId(),wxUser.getUnionId(),date,0);
            meetingUserDao.save(meetingUserEntity);
        }


    }

    @Override
    public List<MeetingUserEntity> getAll(Integer status) {
        List<MeetingUserEntity> list = meetingUserDao.getAll(status);  //查询未扫描到的用户
        //更改已扫描到的用户
        List<Long> ids = new ArrayList<>();
        for (MeetingUserEntity meetingUserEntity : list) {
            ids.add(meetingUserEntity.getId());
        }
//        if(ids.size()>0){
//            meetingUserDao.updateStatus(ids);
//        }
        return list;
    }
}
